<?php
    /**
    * 生成分页的html
    * 
    * @param mixed $total   总量
    * @param mixed $per_page 每页
    * @param mixed $cur_page 当前是第几页，默认值为0,如果是0则url里不显示分页
    * @param mixed $page_parm  表示页面的url参数
    * @note  这个函数只用了$_GET,对input rewrite不兼容，要考虑修正
    */
    function build_nav_bar($total , $per_page, $cur_page ,$page_parm ){
        if($total <= $per_page)
            return null;

        $per_page = intval($per_page)==0?C('DEFAULT_PER_PAGE'):$per_page;
        $cur_page = intval($cur_page);
        $page_parm = empty($page_parm)?'page':$page_parm;

        //这里直接用了GET变量

        $all_pages = ceil($total/$per_page);

        $ret = html::table_header();
        $ret .= '<tr><td>';
        $ret .="当前第".($cur_page+1)."页面&nbsp;&nbsp;";
        $raw_url = $_SERVER['REQUEST_URI'];
        for($i = 0;$i< $all_pages;$i++){
            if($i!=0)$ret .='  | ';

            if(isset($_GET[$page_parm])){
                //preg_replace("/$page_parm=\d+/","/$page_parm=$i/",$url);
                $url = preg_replace("/$page_parm=\d+/","$page_parm=$i",$raw_url);
            }else{

                $url = $raw_url."&$page_parm=$i";
            }
            $ret .= '&nbsp<a href="'.$url.'">'.($i+1).'</a>&nbsp; ';


        }
        $ret .="</td></tr>";
        $ret .=html::table_footer();
        return $ret;
    }
?>
